/*   Copyright (C) 2008  Jeroen v. Schagen & Youri Lima
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Engine.Rendering
{
    public enum TerrainType
    {
        TP_RAW,
        TP_BMP,
        TP_PNG,
        TP_MESH
    }

    [Versioning("P. Reetplug", version = 1.0)]
    public class TerrainManager
    {
        private static TerrainManager _instance;
        private ArrayList _collection;

        private TerrainManager()
        {
            _collection = new ArrayList();
        }

        public bool LoadTerrain(string name, string file, TerrainType type)
        {
            try
            {
                Terrain terrain = new Terrain(name, file, type);

                _collection.Add(terrain);

                return true;
            }

            catch (Exception) { return false; }
        }

        public Terrain GetTerrain(string name)
        {
            foreach (Terrain t in _collection)
            {
                if (t.Name == name)
                {
                    return t.Clone();
                }
            }

            return null;
        }

        public bool Init(Device device)
        {
            foreach (Terrain t in _collection)
            {
                try
                {
                    t.Init(device);
                }

                catch (Exception) { return false; }
            }

            return true;
        }

        // Singleton
        public static TerrainManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new TerrainManager();
                }

                return _instance;
            }
        }
    }
}
